package com.jinke.uang;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.appsflyer.AppsFlyerConversionListener;
import com.appsflyer.AppsFlyerLib;
import com.jinke.uang.app_link.AppLink;
import com.jinke.uang.cache.Acache;
import com.jinke.uang.cache.AcacheKeys;
import com.jinke.uang.http.ParamsFactory;
import com.jinke.uang.utils.DebugLog;

import java.util.Map;

import androidx.multidex.MultiDexApplication;

public class AppApplication extends MultiDexApplication {
    /**
     * 应用上下文
     */
    private static Context mContext;

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        //调用日志
        initArouter();

        DebugLog.setPrintLog(BuildConfig.DEBUG);

        initAppFlyer();
    }

    /**
     * 归因初始化
     */
    private void initAppFlyer() {
        AppsFlyerConversionListener conversionDataListener = new AppsFlyerConversionListener() {
            @Override
            public void onInstallConversionDataLoaded(Map<String, String> map) {
                if (map.containsKey("media_source")) {
                    String mediaSource = map.get("media_source");
                    if (!TextUtils.isEmpty(mediaSource)) {
                        Acache.get(AppApplication.getContext()).put(AcacheKeys.MEDIA_SOURCE,
                                mediaSource);
                        ParamsFactory.unChangeCommonParams = null;
                        ParamsFactory.installCommonParams = null;
                    } else {
                        AppLink.getInstance().init(AppApplication.getContext());
                    }
                }
            }

            @Override
            public void onInstallConversionFailure(String s) {

            }

            @Override
            public void onAppOpenAttribution(Map<String, String> map) {

            }

            @Override
            public void onAttributionFailure(String s) {

            }
        };
        AppsFlyerLib.getInstance().init("HLSTtt5ev9aFsVmhLiGVng", conversionDataListener,
                getApplicationContext());
        AppsFlyerLib.getInstance().setCollectIMEI(false);
        AppsFlyerLib.getInstance().setCollectAndroidID(false);
        AppsFlyerLib.getInstance().startTracking(this);

    }

    private void initArouter() {
        if (BuildConfig.DEBUG) {
            // 打印日志
            ARouter.openLog();
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug();
        }
        // 尽可能早，推荐在Application中初始化
        ARouter.init(this);
    }
}
